<template>
  <view class="page-content">
    <!--#ifdef H5-->
    <wTit
        left-icon="arrowleft"
        background="#F5F7FA"
        color="#666"
        leftText="返回"
        @clickLeft="clickl"
        fontsize="32" />
    <!--#endif-->
    <view class="page-tit">验收材料<text class="tishi">（提示：{{tishi}}）</text></view>
    <view class="page-body">
      <image-upload
          :max="6" name="file"
          :chooseNum="1"
          v-model="formdata.check_img"
          @chooseSuccess="ChooseSuccess"
      @imgDelete="imgDelete">
      </image-upload>
    </view>
  </view>
</template>

<script>
import imageUpload from '@/components/image-upload/image-upload.vue'
import {mapActions, mapGetters} from "vuex";
import {upactivitycheckimgs} from "@/api/index";
import wTit from "@/components/w-tit/w-tit.vue"
export default {
  name: "upload_detail",
  components:{
    imageUpload,wTit
  },
  data() {
    return {
      tishi:'最多上传6张照片',
      formdata:{
        id:'',
        check_img:[],
      },
      activity_id:''
    };
  },
  computed: {
    ...mapGetters(['userInfo','code',''])
  },
  onLoad(option) {
    console.log(123,option)
    if(option.id){
      this.formdata.id = option.id
      let data = option.check_img.split('_');
      if(data != ''){
        this.formdata.check_img = data;
      }
    }
    if(option.activity_id){
      this.activity_id = option.activity_id
    }
  },
  methods: {
    ...mapActions('user', ['setUserInfo', 'setCode', 'set']),
    ChooseSuccess(tempFilePaths) { //选择图片返回
	console.log(4444444,tempFilePaths)
      this.imgUpload(tempFilePaths);
      /*******************************/
    },
    //#ifdef H5
    clickl(){
      uni.navigateTo({url: "/pages/costList/table_detail?id="+this.activity_id});
    },
    //#endif
    imgDelete(row){
      console.log(3344,row)
      let _self = this;
      upactivitycheckimgs(_self.formdata).then(res =>{
        console.log(777,res)
        if(res.code===1){

        }else{
          uni.showToast({
            title: res.msg,
            duration: 2000,
            icon: "none"
          });
        }
      })
    },
    imgUpload(tempFilePaths) {
      let _self = this;
      uni.showLoading({
        title: '上传中'
      });
      let uploadImgs = [];
      tempFilePaths.forEach((item, index) => {
        uploadImgs.push(new Promise((resolve, reject) => {
          let str = 'blob:http://localhost:8080/';
          let imgName= '';
          if(item.indexOf(str)>-1){
            imgName = item.split("blob:http://localhost:8080/")[1]
          }else {
            imgName = item
          }
          uni.uploadFile({
            url: "http://cs.sc.dtc.zone/upload",
            formData: {
              key: imgName + '.jpg',
              fname:imgName + '.jpg',
            },
            filePath: item,
            name: 'file',
            success: (uploadFileRes) => {
              console.log(555,uploadFileRes)
              resolve(uploadFileRes);
              let _res = JSON.parse(uploadFileRes.data);
              _self.formdata.check_img.push(_res.data.url[0]);
              upactivitycheckimgs(_self.formdata).then(res =>{
                console.log(777,res)
                if(res.code===1){

                }else{
                  uni.showToast({
                    title: res.msg,
                    duration: 2000,
                    icon: "none"
                  });
                }
              })
            },
            fail: (err) => {
              console.log(err);
              reject(err);
            },
            complete: () => {
              //uni.hideLoading();
            }
          });
        }))
      })
      Promise.all(uploadImgs).then((results) => { //执行所有需请求的接口
            uni.hideLoading();
          })
          .catch((res, object) => {
            uni.hideLoading();
          });
    },
  }
}
</script>

<style lang="scss">
.page-content{

  .page-tit{
    font-size: 32rpx;
    font-weight: 400;
    line-height: 40rpx;
    color: #2B2B2B;
    margin:30rpx 30rpx 20rpx 30rpx;
    .tishi{
      font-size: 24rpx;
      font-weight: 400;
      line-height: 40rpx;
      color: #2B2B2B;
    }
  }
  .page-body{
    margin: 0 30rpx;
  }
}

</style>